package com.qf.controller;

import com.qf.entity.Student;
import com.qf.entity.StudentVO;
import com.qf.service.StudentService;
import com.qf.util.R;
import lombok.extern.java.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.Console;
import java.util.List;
import java.util.Map;


/**
 * --- ONE PIECE ---
 *
 * @author yangjiangcheng
 * @date 2025/8/15
 * @desc
 */
@RestController
public class StudentController {
    @Autowired
    private StudentService studentService;

    /**
     * 根据id查询一个学生信息
     */

    @GetMapping("/stu/{id}")
    public R findStuById(@PathVariable int id) {
        Student student = studentService.findStuById(id);
        if (student == null) {
            return R.fail(5000, "查无此人");
        }
        return R.success(student);
    }

    /**
     * 查询全部学生信息
     */
    @GetMapping("/stu/list")
    public R studentList() {
        List<Student> list = studentService.studentList();
        return R.success(list);
    }

    /**
     * 通过map实现多参数模糊查询(年龄或者姓名)
     */
    @GetMapping("/stu/search")
    public R listByMap(@RequestParam Map<Integer, String> map) {
        System.out.println("Controller接收前端发送的数据: " + map);
        List<Student> list = studentService.listByMap(map);
        return R.success(list);
    }

    /**
     * 增加学生信息
     */
    @GetMapping("/stu/add")
    public R studentAdd(Student student) {
        int i = studentService.studentAdd(student);
        if (i > 0) {
            return R.success("tjcg");
        }
        return R.fail();
    }

    /**
     * 修改学生信息(全表更新)
     */
    @GetMapping("/stu/update")
    public R studentUpdate(Student student) {
        int i = studentService.studentUpdate(student);
        if (i > 0) {
            return R.success("gxcg");
        }
        return R.fail();
    }

    /**
     * 删除学生信息,根据id删除
     */
    @GetMapping("/stu/delete")
    public R studentDelete(int id) {
        int i = studentService.studentDelete(id);
        if (i > 0) {
            return R.success("scchg");
        }
        return R.fail();
    }

    /**
     * 批量删除学生信息,删除sql语句的动态效果
     */
    @GetMapping("/stu/delete/batch")
    public R deleteBatch(@RequestParam List<Integer> ids) {
        System.out.println("批量删除,接收到前端的ids:" + ids);
        studentService.deleteBatch(ids);
        return R.success();
    }

    /**
     * 查询所有学生信息以及关联的班级信息(一对一)
     */
    @GetMapping("/stu/search/classroom")
    public R findAllStudentAndClassroom() {
        List<StudentVO> list = studentService.findAllStudentAndClassroom();
        return R.success(list);
    }

    /**
     * 查询一个学生信息以及所学习的所有学科信息(一对多)
     */
    @GetMapping("/stu/search/subjects")
    public R findStudentAndAllSubjectByStuid(int sid) {
        StudentVO studentVO = studentService.findStudentAndAllSubjectByStuid(sid);
        return R.success(studentVO);
    }

    /**
     * 根据id查询1个学生信息以及关联的所有学科信息以及每个学科关联的老师信息(一对多对一)
     */
    @GetMapping("stu/subjects/teacher")
    public R findStudentAndAllSubjectAndTeacherByStuid(int sid) {
        StudentVO studentVO = studentService.findStudentAndAllSubjectAndTeacherByStuid(sid);
        return R.success(studentVO);
    }

    /**
     * 查询学生信息以及关联的教室信息(拆解多表联查)
     *
     * @return
     */
    @GetMapping("/stu/search/classroom/v2")
    public R findAllStudentAndClassroomV2() {
        List<StudentVO> list = studentService.findAllStudentAndClassroomV2();
        return R.success(list);
    }

    /**
     * 查询一个学生信息以及所学习的所有学科信息(拆解多表联查)
     */
    @GetMapping("/stu/search/subjects/v2")
    public R findStudentAndAllSubjectByStuidV2(int sid){
        StudentVO studentVO = studentService.findStudentAndAllSubjectByStuidV2(sid);
        return R.success(studentVO);
    }


}
